//
//  Options.m
//  SKStock
//
//  Created by thPark on 11. 1. 12..
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import "Options.h"

NSMutableDictionary* _optionUser[OPTION_USER_COUNT];

@implementation Options

+ (void)initUserDefault {
	[[NSUserDefaults standardUserDefaults] registerDefaults:[NSDictionary dictionaryWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"Options" ofType:@"plist"]]];
	
	for(int i = 0; i < OPTION_USER_COUNT; i++) {
		_optionUser[i] = [[NSMutableDictionary alloc] initWithDictionary:
						   [[NSUserDefaults standardUserDefaults] objectForKey:
							[NSString stringWithFormat:@"Option%d", i + 1]]];

//		LOG_TRACE(@">> %d Options\n", i);
//		LOG_TRACE(@"%@\n", _optionUser[i] );
//		LOG_TRACE(@"<<\n");
	}
}

+ (id)getIDValue:(OptionUser)user valueName:(NSString *)name {
	id obj = [_optionUser[(int)user] objectForKey:name];
	if(obj == nil) {
		NSString * key = [NSString stringWithFormat:@"Option%d", (int)user + 1];
		[[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
		NSDictionary * dic = [[NSUserDefaults standardUserDefaults] objectForKey:key];
		obj = [dic objectForKey:name];
		if(obj) {
			[_optionUser[(int)user] setObject:obj forKey:name];
			[[NSUserDefaults standardUserDefaults] setObject:_optionUser[(int)user] forKey:key];
		}
	}
	return obj;
}

+ (void)setIDValue:(OptionUser)user valueName:(NSString *)name value:(id)value {
	[_optionUser[(int)user] setObject:value forKey:name];
	NSString * key = [NSString stringWithFormat:@"Option%d", (int)user + 1];
	[[NSUserDefaults standardUserDefaults] setObject:_optionUser[(int)user] forKey:key];
	[[NSUserDefaults standardUserDefaults] synchronize];
}

+ (NSString *)getValue:(OptionUser)user valueName:(NSString *)name {
	return [self getIDValue:user valueName:name];
}

+ (void)setValue:(OptionUser)user valueName:(NSString *)name value:(NSString *)value {
	if (value == nil || name == nil) {
		return;
	}
	[self setIDValue:user valueName:name value:value];
}

+ (void)clearValue:(OptionUser)user valueName:(NSString *)name {
	[_optionUser[(int)user] removeObjectForKey:name];
	NSString * key = [NSString stringWithFormat:@"Option%d", (int)user + 1];
	[[NSUserDefaults standardUserDefaults] setObject:_optionUser[(int)user] forKey:key];
	[[NSUserDefaults standardUserDefaults] synchronize];
}

//+ (NSString*)getLoginID{
//	return [self getValue:OptionUser3 valueName:@"UserID"];
//}

+ (NSDictionary *)getDictionaryValue:(OptionUser)user valueName:(NSString *)name {
	return [self getIDValue:user valueName:name];
}

+ (void)setDictionaryValue:(OptionUser)user valueName:(NSString *)name value:(NSDictionary *)value {
	[self setIDValue:user valueName:name value:value];
}

@end
